﻿using glb;
using log4net;
using NPOI.SS.Formula.Functions;
using PIMS.Interface;
using PIMS.Uil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PIMS.Utils
{
    /// <summary>
    /// LOG4NET配置文件是log4net.config
    /// 绑定配置在AssemblyInfo.cs里
    /// [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
    /// </summary>
    internal class Log4NetHelper : ILog4net
    {
        //这里的 loginfo 和 log4net.config 里的名字要一样
        private readonly ILog log_info = log4net.LogManager.GetLogger("Info");

        //这里的 logerror 和 log4net.config 里的名字要一样
        private readonly ILog log_error = log4net.LogManager.GetLogger("Error");


        public MessageQueue<LogMsgStruct> LogQueue = new MessageQueue<LogMsgStruct>();

        public Log4NetHelper()
        {
            new Thread(new ThreadStart(Thread_WriteLog)) { IsBackground = true }.Start();
        }

        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public void Info(string message)
        {
            if (log_info.IsInfoEnabled)
            {
                LogMsgStruct logMsg = new LogMsgStruct(message,"Info");

                LogQueue.Enqueue(logMsg);
            }
        }

        /// <summary>
        /// 错误日志带异常
        /// </summary>
        /// <param name="message">错误日志</param>
        public void Error(string message, Exception ex)
        {
            if (log_error.IsErrorEnabled)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("【UserMessage】" + message + "\r\n");
                sb.Append("【ErrorType】" + ex.GetType()+ "\r\n");
                sb.Append("【TargetSite】" + ex.TargetSite + "\r\n");
                sb.Append("【Message】" + ex.Message + "\r\n");
                sb.Append("【Source】" + ex.Source + "\r\n");
                sb.Append("【StackTrace】" + ex.StackTrace);

                LogMsgStruct logMsg = new LogMsgStruct(sb.ToString(), "Error");
                LogQueue.Enqueue(logMsg);
            }
        }

        /// <summary>
        /// 错误日志不带异常
        /// </summary>
        /// <param name="message">错误日志</param>
        public void Error(string message)
        {
            if (log_error.IsErrorEnabled)
            {
                LogMsgStruct logMsg = new LogMsgStruct(message, "Error");

                LogQueue.Enqueue(logMsg);
            }
        }


        public void Info(string message, Enum logGroup,int plcIndex)
        {
            if (log_info.IsInfoEnabled)
            {
                LogMsgStruct logMsg = new LogMsgStruct(message, logGroup, plcIndex, "Info");

                LogQueue.Enqueue(logMsg);
            }
        }

        public void Error(string message, Enum logGroup,int plcIndex,Exception ex)
        {
            if (log_error.IsErrorEnabled)
            {
                //LogMsgStruct logMsg = new LogMsgStruct(message + " " + ex.Message + " " + ex.StackTrace, logGroup, plcIndex, "Error");

                StringBuilder sb = new StringBuilder();
                sb.Append("【UserMessage】" + message + "\r\n");
                sb.Append("【ErrorType】" + ex.GetType() + "\r\n");
                sb.Append("【TargetSite】" + ex.TargetSite + "\r\n");
                sb.Append("【Message】" + ex.Message + "\r\n");
                sb.Append("【Source】" + ex.Source + "\r\n");
                sb.Append("【StackTrace】" + ex.StackTrace);

                LogMsgStruct logMsg = new LogMsgStruct(sb.ToString(), logGroup, plcIndex, "Error");
                LogQueue.Enqueue(logMsg);
            }
        }

        public void Error(string message, Enum logGroup,int plcIndex)
        {
            if (log_error.IsErrorEnabled)
            {
                LogMsgStruct logMsg = new LogMsgStruct(message, logGroup, plcIndex, "Error");

                LogQueue.Enqueue(logMsg);
            }
        }


        private void Thread_WriteLog()
        {
            while (true)
            {
                try
                {
                    LogMsgStruct logMsg = LogQueue.Dequeue();
                    if (logMsg != null)
                    {
                        if(logMsg.logLevel=="Error")
                        {
                            log_error.Error(logMsg.msg);
                        }
                        else
                        {                            
                            log_info.Info(logMsg.msg);
                        }

                        // SQLiteDBHelper.DBLog(logMsg.msg, logMsg.logLevel);
                        SQLiteDBHelper.DBLog(logMsg.msg, logMsg.group, logMsg.plcIndex,logMsg.time,logMsg.logLevel);

                        SQLiteDBHelper.DBDelete(365);
                    }
                }
                catch
                {
                }
                Thread.Sleep(50);
            }
        }

    }

    internal class LogMsgStruct
    {

        public LogMsgStruct(string msg,string level)
        {
            this.msg = msg;
            this.logLevel = level;
            this.group = "默认";
            this.time = DateTime.Now;
        }

        public LogMsgStruct(string msg, Enum logGroup, int plcIndex,string level)
        {
            this.msg = msg;
            this.logLevel = level;
            // this.group = logGroup.ToString();
            this.group = Common.logGroupName[logGroup.ToString()];
            this.plcIndex= plcIndex;
            this.time=DateTime.Now;
        }

        public string msg { set; get; }

        public string group { set; get; }

        public int plcIndex { set; get; }

        public string logLevel { set; get; }

        public DateTime time { set; get; }
    }
}
